<script language="javascript">
	function viewproductByCat(CatID,LID,SID,HID){
		window.location='?mod=product.listall&CatID=' + CatID + '&LID=' + LID + '&SID=' + SID + '&HID=' + HID;
	}
	function viewProductByLocation(LID,CatID,SID,HID){
		window.location='?mod=product.listall&LID=' + LID + '&CatID=' + CatID + '&SID=' + SID + '&HID=' + HID;
	}
	function viewProductByStatus(SID,CatID,LID,HID){
		window.location='?mod=product.listall&SID=' + SID + '&CatID=' + CatID + '&SID=' + SID + '&HID=' + HID;
	}
	function viewProductByHOT(HID,CatID,LID,SID){
		window.location='?mod=product.listall&HID=' + HID + '&SID=' + SID + '&CatID=' + CatID + '&SID=' + SID;
	}
</script>
<?
if(isset($_GET['method']))
{
	$method = $_GET['method'];
	if($method=="del"){
		$sListId="";
		if(isset($_POST["chon"]))$sListId=$_POST["chon"];
		$sql="delete from ".$prefix."sanpham where t_id in('".str_replace(",","','",$sListId)."')";
		mysql_query($sql);
		Redirect("Bạn đã xoá thành công","?mod=product.listall");
	}else if($method=="act"){
		$IdList=$_GET["id"];
		$act=$_GET["act"];
		if($act==1) $iact=0;else $iact=1;
		$sql="update ".$prefix."sanpham set t_hienthi=$iact where t_id=$IdList";
		mysql_query($sql);
		Redirect("","?mod=product.listall");		
	}else if($method=="delete"){
		$IdList=$_GET["id"];
		$sql="delete from ".$prefix."sanpham where t_id=$IdList";
		mysql_query($sql);
		Redirect("Đã xóa thành công","?mod=product.listall");		
	}else if($method=="hot"){
		$IdList=$_GET["id"];
		$hot=$_GET["hot"];
		if($hot==1) $ihot=0;else $ihot=1;
		$sql="update ".$prefix."sanpham set t_noibat=$ihot where t_id=$IdList";
		mysql_query($sql);
		Redirect("","?mod=product.listall");		
	}	
}

$sql = "select * from ".$prefix."sanpham WHERE 1";

if(isset($_GET['CatID'])){
	$CatID = intval($_GET['CatID']);
	if($CatID != '-1'){
		$sql .= " AND t_loaisanpham = '".$CatID."'";
	}
}else{
	$CatID = '-1';
}

if(isset($_GET['LID'])){
	$LID = intval($_GET['LID']);
	if($LID != '-1'){
		$sql .= " AND t_tinhthanh = '".$LID."'";
	}
}else{
	$LID = '-1';
}


if(isset($_GET['SID'])){
	$SID = intval($_GET['SID']);
	if($SID != 3){
		$sql .= " AND t_hienthi = '".$SID."'";
	}
}else{
	$SID = 3;
}

if(isset($_GET['HID'])){
	$HID = intval($_GET['HID']);
	if($HID != 3){
		$sql .= " AND t_noibat = '".$HID."'";
	}
}else{
	$HID = 3;
}

	///////Paging
	
	$pp=10; 
	$parr=array();
	$parr=paging($sql,$pp);
	$page=$parr[0];
	$limitvalue=$parr[1];
	$numofpages=$parr[2];
	$sql .= " order by t_ngaydang DESC";
	$sql.=" LIMIT $limitvalue, $pp";
	$rs = mysql_query($sql) or die('Not query');
	$i=0;
	$isexits=false;
	
	if(mysql_num_rows($rs)) $isexits=true;
?>
<div class="newsList-path"></div>
	<div style="margin-bottom:5px; font-size:14px;">
	<?
	$sql = "select count(*) as total from ".$prefix."sanpham".$where."";
	$result = mysql_query($sql) or die("Can't connect database");
	$row = mysql_fetch_array($result);
	?>
	 Có <?=$row['total']?> sản phẩm được tìm thấy </div>
	<div align="right" class="newsList-search-up">
	<strong>Lọc sản phẩm</strong>&nbsp;
	<select name="FilterByCat" id="CatID" onchange="return(viewproductByCat(this.value,'<?=$LID?>','<?=$SID?>','<?=$HID?>'));">
	<option value="-1" <? if($CatID == "-1") {?> selected="selected" <? } ?>>-----Danh mục-----</option>
	<?
	$sql = "select * from ".$prefix."loaisanpham order by c_id ASC";
	$result = mysql_query($sql) or die("Can't connect database");
	while($row = mysql_fetch_array($result))
	{
	?>	
	<option value="<?=$row['c_id']?>" <? if($CatID == $row[c_id]){?> selected="selected" <? }?>><?=$row['c_ten']?></option>
	<? }?>
	  </select>
	  &nbsp;<select name="FilterByTinhThanh" id="LID" onchange="return(viewProductByLocation(this.value,'<?=$CatID?>','<?=$SID?>','<?=$HID?>'));">
	<option value="-1" <? if($LID == '-1'){ ?> selected="selected" <? } ?>>--Khu vực--</option>
	<?
	$sql = "select * from ".$prefix."tinhthanh order by tt_ten ASC";
	$result = mysql_query($sql) or die("Can't connect database");
	while($row = mysql_fetch_array($result))
	{
	?>	
	<option value="<?=$row['tt_id']?>" <? if($LID == $row[tt_id]){?> selected="selected" <? } ?>><?=$row['tt_ten']?></option>
	<? }?>	
	  </select>
	  &nbsp;<select name="FilterByStatus" id="SID" onchange="return(viewProductByStatus(this.value,'<?=$CatID?>','<?=$LID?>','<?=$HID?>'));">
	<option value="3" <? if($SID == 3){?> selected="selected" <? }?>>-----Trạng thái-----</option>
	<option value="1" <? if($SID == 1){?> selected="selected" <? }?>>Đã kích hoạt</option>
	<option value="0" <? if($SID == 0){?> selected="selected" <? }?>>Chưa kích hoạt</option>
	  </select>
	   &nbsp;<select name="FilterByHOT" id="HID" onchange="return(viewProductByHOT(this.value,'<?=$CatID?>','<?=$LID?>','<?=$SID?>'));">
	<option value="3" <? if($HID == 3){?> selected="selected" <? }?>>------Tất cả------</option>
	<option value="1" <? if($HID == 1){?> selected="selected" <? }?>>Nổi bật</option>
	<option value="0" <? if($HID == 0){?> selected="selected" <? }?>>Không nổi bật</option>
	  </select>
	</div>
	<form name="frmListSp" method="post" action="?mod=product.listall&method=del" onSubmit="return checkInput(this,'Bạn hãy chọn ít nhất một giá trị','Bạn chắc chắn muốn xoá không');">
	<input type="hidden" name="chon" />
	<table width="100%" border="1" bordercolor="#B2DAEE" cellpadding="4">
		<tr class="newsList-content-head">
		<td width="4%" class="newsList-content-view"><input type="checkbox" name="chkall" value="0" <? if($isexits) echo "onClick=\"docheck(this.form,0);\""?>></td>
			<td width="39%" class="newsList-content-title">Tiêu đề</td>

			<td width="6%" class="newsList-content-view"><div align="center">Hiển thị </div></td>
			<td width="6%" class="newsList-content-view"><div align="center">Nổi bật </div></td>
			<td width="17%" class="newsList-content-view"><div align="center">Loại sản phẩm </div></td>
			<td width="15%" class="newsList-content-position">Khu vực</td>
			<td width="13%" class="newsList-content-position">Tùy chọn </td>
		</tr>
				<?
				while($rows = mysql_fetch_array($rs))
				{
					$i++;
					$t_id = $rows['t_id'];
					$t_loaisanpham = $rows['t_loaisanpham'];
					$t_nguoidang = $rows['t_nguoidang'];
					$t_tieude = $rows['t_tieude'];
					$t_hinhanh = $rows['t_hinhanh'];
					$t_tinhthanh = $rows['t_tinhthanh'];
					$t_ngaydang = $rows['t_ngaydang'];
					$t_solanxem = $rows['t_solanxem'];
					$t_noibat = $rows['t_noibat'];
					$t_hienthi = $rows['t_hienthi'];
				?>				
		<tr onmouseover="this.bgColor='#FEF2CB';" onmouseout="this.bgColor='#FFFFFF';" class="newsList-content-list" <? if($i%2==0) echo "bgcolor='#f2fff2'";?>>
			<td class="newsList-content-title"><input type="checkbox" name="Xoa" value="<?=$t_id?>" onClick="docheckone(this.form);"></td>
			<td class="newsList-content-title">
            	<table cellpadding="0" cellspacing="0" width="100%">
                <tr>
					<td colspan="2">
						<a class="newsList-link newsList-name" href="">
						<b><?=$t_tieude?></b></a>&nbsp;<i>(Xem:<?=$t_solanxem?>)</i>	</td>
                <tr>
                    <td><div class="newsList-user-id" >Người đăng: <a class="newslist-normal-link" href="#"><?=tenNguoiDung($t_nguoidang);?></a></div></td>
                	<td align="right"><div class="newsList-time">Ngày đăng: <span class="newsList-date"><?=format_date($t_ngaydang);?></span></div></td>
                </tr>
                </table>				</td>
			<td class="newsList-content-view"><div align="center"><a href="?mod=product.listall&method=act&act=<?=$t_hienthi;?>&id=<?=$t_id?>"><?=ImgActive($t_hienthi);?></a></div></td>
			<td class="newsList-content-view"><div align="center"><a href="?mod=product.listall&method=hot&hot=<?=$t_noibat;?>&id=<?=$t_id?>"><?=ImgActive($t_noibat);?></a></div></td>
			<td class="newsList-content-view"><div align="center"><?=getLoaiSanPham($t_loaisanpham);?></div></td>
			<td class="newsList-content-position"><a class="newsList-link" href="?mod=product.listall&amp;location=<?=$t_tinhthanh?>&amp;portal=<?=$mod?>">
			  <?=layTinhThanh($t_tinhthanh);?>
			  </a></td>
			<td class="newsList-content-position"><div align="center"><a href="?mod=product.add&amp;method=edit&amp;id=<?=$t_id?>" class="menu"><img src="images/edit.gif" border="0" /></a>&nbsp; <a href="?mod=product.listall&amp;method=delete&amp;id=<?=$t_id?>" class="menu" onclick="return confirm('Bạn có chắc chắn muốn xóa không?')"><img src="images/delete_on.gif" border="0" /></a></div></td>
        </tr>
		<? }?>
	<tr>
   <td colspan="9" align="center">
   <input type="submit" name="submit" value="   Xoá   " class="Button">
   <input type="reset" value="Làm lại" class="Button">   </td>
   </tr>
</table>
	</form>
	<div class="newsList-pagging">
	<div class="raovat-paging-bound">
	<span style="padding:5px;">
	<p align="center" class="font">
			<?php echo page_div( "?mod=product.listall&p=%d_pg", "10", ceil ( $numofpages ), $page ); ?>
	</p>
	</span>
	</div>
	</div>
	<div class="newsList-button-back"><a href="javascript: history.back()">[Quay lại]</a></div>